import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
  {
    path: "/",
    redirect: '/login'
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/login/index.vue")
  },
  {
    path: "/home",
    name: "Home",
    component: () => import("../views/home/index.vue"),
    children: [
      {
        path: "/home",
        redirect: "/wolcome"
      },
      {
        path: "/wolcome",
        component: () => import("../views/wolcome.vue")
      },
      {
        path: "/users",
        component: () => import("../views/users/users.vue")
      },
      {
        path: "/roles",
        component: () => import("../views/rights/roles.vue")
      },
      {
        path: "/rights",
        component: () => import("../views/rights/rights.vue")
      },
      {
        path: "/goods",
        component: () => import("../views/goods/goods.vue")
      },
      {
        path: "/add",
        component: () => import("../views/goods/addAndEditGoods.vue")
      },
      {
        path: "/params",
        component: () => import("../views/goods/params.vue")
      },
      {
        path: "/categories",
        component: () => import("../views/goods/categories.vue")
      },
      {
        path: "/orders",
        component: () => import("../views/orders/orders.vue")
      },
      {
        path: "/reports",
        component: () => import("../views/reports/reports.vue")
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: routes
})

router.beforeEach((to, from, next) => {
  if (window.sessionStorage.getItem('hmds-token')) {
    if(to.path == '/login') {
      next('/home')
    } else {
      next()
    }
  } else {
    if (to.path == '/login') {
      next()
    } else {
      next('/login')
    }
  }
})

export default router